<?php
	include('header.php');
?>		
<script>
	
	$(function() {
		
		$("#tdnNo").focus();
		
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
	
		$( "#dialog-confirm" ).dialog({
			autoOpen: false,
			resizable: false,
			height:150,
			modal: true,
			buttons: {
				"Cancel this package": function() {
					$( this ).dialog( "close" );
					$("#cancelPackageCfm").click();
				},
				"No": function() {
					$( this ).dialog( "close" );
				}
			}
		});
		
		$("#cancelPackage").button()
			.click(function() {
				if($("#tdnNo" ).val() != ''){
					$("#dialog-confirm" ).dialog( "open" );
				}
				return false;
		});
		
	});
</script>

		<div id="dialog-confirm" title="Cancel this package?">
			<p>
				<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
					These package will be cancelled and will auto add new package by same TDN No.! Are you sure?</p>
		</div>
		<!--[if !IE]>start content<![endif]-->					
		<div id="content">
			<div class="inner">
				
				<!--[if !IE]>start section<![endif]-->
				<?php

				

				?>
		
				<div class="section">
					
					<!--[if !IE]>start title wrapper<![endif]-->
					<div class="title_wrapper">
						<span class="title_wrapper_top"></span>
						<div class="title_wrapper_inner">
							<span class="title_wrapper_middle"></span>
							<div class="title_wrapper_content"> 
								<h2>Package - Update Cancel For Today (<?php echo date("l d-m-Y"); ?>)</h2>
							</div>
						</div>
						<span class="title_wrapper_bottom"></span>
					</div>
					<!--[if !IE]>end title wrapper<![endif]-->
					
					<!--[if !IE]>start section content<![endif]-->
					<div class="section_content">
						<span class="section_content_top"></span>
						
						<div class="section_content_inner">
							<?php
								 
								$tdnNo = "";  
								$isCompleted = "";
								if(isset($_POST['reset'])){
									$_POST = "";
								}

								if(isset($_POST['cancelPackageCfm'])){ 
									$tdnNo = mysql_real_escape_string($_POST['tdnNo']); 
									$tpd_status_remarks = mysql_real_escape_string($_POST['tpd_status_remarks']); 
									$resultUpdateTdnNo = "";
									
									if($tdnNo != "" ) {
										$qTotTPD= "SELECT COUNT(*)  FROM transport_package_delivering 
														WHERE  
														tpd_tdn_no = '$tdnNo' 
														AND tpd_status = 'TPD01' 
														AND tpd_trn_id IS NOT NULL 
													 ";
										$totalTPD = $db->sql_total($qTotTPD);
										
										
										if($totalTPD == 1) {
										
											$qTPD= "SELECT * FROM transport_package_delivering 
															WHERE  
															tpd_tdn_no = '$tdnNo' 
															AND tpd_status = 'TPD01' 
															AND tpd_trn_id IS NOT NULL 
														 ";
											$resTPD = $db->sql_list($qTPD);
											
											$tpdID = $resTPD['tpd_id'];
											$tpd_sender_ent_id = $resTPD['tpd_sender_ent_id'];
											$tpd_receiver_ent_id = $resTPD['tpd_receiver_ent_id'];
											$tpd_do_ref = $resTPD['tpd_do_ref'];
											$tpd_qty = $resTPD['tpd_qty'];
											$tpd_qty_type = $resTPD['tpd_qty_type'];
											$tpd_type_pallet = $resTPD['tpd_type_pallet'];
											
											if($tpd_status_remarks == '' || $tpd_status_remarks == null)
												$tpd_status_remarks = 'Cancel Package';
											$tpd_status = 'TPD90';
											$sqlUpdPTD= "UPDATE  transport_package_delivering 
															set tpd_status = '$tpd_status',
																tpd_status_remarks = '$tpd_status_remarks',
																tpd_delivered_cancel_date = now(),
																tpd_modified_date = now(),
																tpd_modified_by = '$_SESSION[log_userlogin_id]'
															WHERE 	
																tpd_id = '$tpdID' ";
											$resUpdPTD = $db->sql_query($sqlUpdPTD);
											
											$sqlInsertLog = " INSERT INTO tpd_status_log 
														(tsl_tpd_id,tsl_tdn_no,tsl_status,tsl_status_remarks,tsl_created_date,tsl_created_by) 
															VALUES
														('$tpdID','$tdnNo','$tpd_status','update status from Start Delivering to Cancel',NOW(),'$_SESSION[log_userlogin_id]')";
											$resInsertLog= $db->sql_query($sqlInsertLog);
											
											
											$tpd_status="TPD00";
											$tpd_status_remarks="Add new package set Initial Status because cancel another package";
											$sqlInsertTPD = "  INSERT INTO transport_package_delivering 
															(tpd_cancel_old_id,tpd_sender_ent_id,tpd_receiver_ent_id,tpd_tdn_no,tpd_do_ref,
																tpd_qty,tpd_qty_type,tpd_type_pallet,
																tpd_status,tpd_status_remarks,tpd_created_date,tpd_created_by) 
															VALUES
															('$tpdID','$tpd_sender_ent_id','$tpd_receiver_ent_id','$tdnNo','$tpd_do_ref',
																'$tpd_qty','$tpd_qty_type','$tpd_type_pallet',
																'$tpd_status','$tpd_status_remarks',now(),'$_SESSION[log_userlogin_id]')";
											$resInsertTPD = $db->sql_query($sqlInsertTPD);
											$TPDNewid = $db->sql_insertID();
											
											$sqlInsertLog2 = " INSERT INTO tpd_status_log 
														(tsl_tpd_id,tsl_tdn_no,tsl_status,tsl_status_remarks,tsl_created_date,tsl_created_by) 
															VALUES
														('$TPDNewid','$tdnNo','$tpd_status','$tpd_status_remarks',NOW(),'$_SESSION[log_userlogin_id]')";
											$resInsertLog2= $db->sql_query($sqlInsertLog2);
																
											if($resUpdPTD && $resInsertLog && $resInsertTPD && $resInsertLog2){
												$resultUpdateTdnNo = "Cancel Successful for TDN N0. $tdnNo. New package auto added.<br/> You may re-assign transport for this TDN N0. $tdnNo.  ";
												$isCompleted = 1;
											}
										}else {
											$resultUpdateTdnNo = "Search for '$tdnNo' not found!";
											$isCompleted = "";
										}
									}
								}
								
							?>
							<form action="" method="POST" class="search_form">
							
							
							<!--[if !IE]>start fieldset<![endif]-->
							<fieldset>
								<!--[if !IE]>start forms<![endif]-->
								<div class="forms">
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>TDN No. :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<input  class="text" id="tdnNo" name="tdnNo" value="" type="text"  />
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<label>Remarks :</label>
									<div class="inputs">
										<span class="input_wrapper" style="width:300px;">
											<textarea name="tpd_status_remarks" style="width: 297px; height: 49px;"></textarea>
										</span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<!--[if !IE]>start row<![endif]-->
								<div class="row">
									<div class="inputs">
										<span class="button blue_button search_button"><span><span><em>Cancel Package</em></span></span><input id="cancelPackage" name="cancelPackage" type="submit" /></span>
									</div>
								</div>
								<!--[if !IE]>end row<![endif]-->
								
								<input style="display:none;" id="cancelPackageCfm" name="cancelPackageCfm" type="submit" />
								
								
								
								</div>
								<!--[if !IE]>end forms<![endif]-->
								
							</fieldset>
							<!--[if !IE]>end fieldset<![endif]-->
							
							
							</form>
							
							<div class="table_tabs_menu">
								<h3><?php  
									//show status result
									echo $resultUpdateTdnNo; ?> </h3>
								<br />
							</div>
							
							<?php if($isCompleted == 1 && $tdnNo != ''){ ?>
							<!--[if !IE]>start table_wrapper<![endif]-->
							<div class="table_wrapper">
								<div class="table_wrapper_inner">
								
								<?php  
									
									/* Get data. */
									$qList= "SELECT t.*, tpd.*, s.ent_id AS sid ,
											s.ent_short_name AS sname, 
											r.ent_id AS rid, r.ent_short_name AS rname, r.ent_add_postcode AS rpostcode
											FROM transport t , transport_package_delivering tpd , entity s, entity r
												WHERE t.trn_id = tpd.tpd_trn_id
												AND tpd.tpd_sender_ent_id=s.ent_id
												AND tpd.tpd_receiver_ent_id=r.ent_id
												AND tpd.tpd_tdn_no = '$tdnNo' ";
									$rList = $db->sql_fetch($qList);			

								?>
								<table cellpadding="0" cellspacing="0" width="100%">
									<tbody>
									<tr>
										<th style="width: 10px;">Bil.</th>
										<th style="width: 50px;">TDN No.</th>
										<th style="width: 50px;">DO No.</th>
										<th style="width: 50px;">Quatity</th>
										<th>Sender</th>
										<th>Receiver</th>
										<th>Destination</th>
										<th>Status</th>
										<th>Action</th>
										
									</tr>
								<?php
									$num =$start;
									foreach ($rList as $i) {
										$num++;
										$colorRow = "first";
										if($num%2==0)
											$colorRow = "second"; 
										else
											$colorRow = "first";
										
										$qCity= "SELECT * from lookup_postcode where postcode = '$i[rpostcode]' ";
										$rCity = $db->sql_list($qCity);
				
										echo '	
											<tr class="'.$colorRow.'">
												<td>'.$num.'</td>
												<td>'.$i['tpd_tdn_no'].'</td>
												<td>'.$i['tpd_do_ref'].'</td>
												<td>'.$i['tpd_qty'].' '.$i['tpd_qty_type'].'</td>
												<td>'.$i['sname'].'</td>
												<td>'.$i['rname'].'</td>
												<td>'.$i['rpostcode'].' '.$rCity['post_office'].'</td>
												<td>'.getStatus($i['tpd_status']).'</td>
												<td>
													<div class="actions_menu" >
														<ul>
															<li><a class="details" href="packageview.php?id='.$i['tpd_id'].'">View</a></li>
														</ul>
													</div>
												</td>
											</tr>';
										
									}
								?>
								</tbody></table>
								</div>
								</div>
								
							</div><!--[if !IE]>end table_wrapper<![endif]-->
							<?php } ?>
							
						
						<span class="section_content_bottom"></span>
					</div>
					<!--[if !IE]>end section content<![endif]-->
				</div>
				<!--[if !IE]>end section<![endif]-->
				
					
				
			</div>
		</div>
		<!--[if !IE]>end content<![endif]-->

<?php
	include('footer.php');
?>